<html>
<head><meta charset="utf-8"><title>Create Const Array in Mir Opt · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Create.20Const.20Array.20in.20Mir.20Opt.html">Create Const Array in Mir Opt</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238085055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Create%20Const%20Array%20in%20Mir%20Opt/near/238085055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Create.20Const.20Array.20in.20Mir.20Opt.html#238085055">(May 10 2021 at 04:17)</a>:</h4>
<p>Is there a convenient way to create a const array inside of a mir opt? I was working on implementing an opt outlined <a href="https://hackmd.io/@ft4bxUsFT5CEUBmRKYHr7w/rJM8BBPzD">here</a>, and it's necessary to build a const array for use at runtime. It seems very easy to create scalars, but creating an array seems to require an allocation, and the comment also seems to suggest it is only for <code>&amp;[u8]</code> and <code>&amp;str</code>, but I was wondering if I just missed something obvious. If not, I was wondering if it makes sense to add a variant to <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/interpret/value/enum.ConstValue.html">ConstValues</a> for arrays?</p>



<a name="238109483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Create%20Const%20Array%20in%20Mir%20Opt/near/238109483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Create.20Const.20Array.20in.20Mir.20Opt.html#238109483">(May 10 2021 at 09:13)</a>:</h4>
<p>There is no convenient way. What you have to do is create an <code>Allocation</code>, fill it in with your values, then use some function on <code>TyCtxt</code> whose name I forget (use rustdoc to search for a function taking an <code>Allocation</code> and returning an <code>&amp;'tcx Allocation</code>). Then use that to create a <code>ConstValue::ByRef</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>